package com.shouhuobao.bhi.stamp;

import java.util.ArrayList;
import java.util.HashMap;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.collectplus.express.R;
import com.collectplus.express.logic.AppLogic;
import com.collectplus.express.model.StampShopBean;
import com.collectplus.express.model.StampStoreBean;

import droid.frame.activity.base.BaseAdapterExt;
import droid.frame.utils.lang.Str;

/**
 * 邮票商城
 * 
 * @author kaka<br>
 *         2017年4月20日
 */
public class StampStoreAdapter extends BaseAdapterExt<StampStoreBean> {
	/**
	 * 当前正在使用的ID
	 */
	private int selectId;

	private boolean showFooter = false;

	private StampStoreActivity activity;

	public StampStoreAdapter(ArrayList<StampStoreBean> items, StampStoreActivity context) {
		super(items, context);
		//
		if (context instanceof StampStoreActivity) {
			// setEmptyView(R.layout.coupon_empty);
		}
		activity = context;
	}

	public int getSelectId() {
		return selectId;
	}

	public void setSelectId(int selectId) {
		this.selectId = selectId;
	}

	/**
	 * 是否显示底部Footer
	 * 
	 * @param showFooter
	 */
	public void setShowFooter(boolean showFooter) {
		this.showFooter = showFooter;
	}

	@Override
	public int getCount() {
		return super.getCount() + (showFooter ? 1 : 0);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (hasEmptyView()) {
			View layout = super.getView(position, convertView, parent);
			return layout;
		}
		ViewHolder holder = null;
		if (convertView == null || convertView.getTag() == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.stamp_store_item, parent, false);
			holder = new ViewHolder();
			holder.stampImg = (ImageView) convertView.findViewById(R.id.stamp_img);
			holder.stampName = (TextView) convertView.findViewById(R.id.stamp_name);
			holder.desc = (TextView) convertView.findViewById(R.id.stamp_desc);
			holder.deadline = (TextView) convertView.findViewById(R.id.stamp_outdate);
			holder.category = (TextView) convertView.findViewById(R.id.stamp_category);
			holder.price0 = (LinearLayout) convertView.findViewById(R.id.stamp_choose0);
			holder.price1 = (LinearLayout) convertView.findViewById(R.id.stamp_choose1);
			holder.price2 = (LinearLayout) convertView.findViewById(R.id.stamp_choose2);
			holder.price3 = (LinearLayout) convertView.findViewById(R.id.stamp_choose3);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		StampStoreBean storeBean = items.get(position);
		holder.stampName.setText(storeBean.getName());
		holder.desc.setText("使用条件:" + storeBean.getCondition());
		holder.deadline.setText("(有效期:" + storeBean.getValidPeriod() + "天)");
		holder.category.setText(storeBean.getSeries());
		if (storeBean != null && !Str.isEmpty(storeBean.getImage())) {
			AppLogic.getInstance().loadImage(storeBean.getImage(), holder.stampImg);
		}

		ArrayList<StampShopBean> stampShop = storeBean.getStampList();
		holder.price0.setVisibility(View.INVISIBLE);
		holder.price1.setVisibility(View.INVISIBLE);
		holder.price2.setVisibility(View.INVISIBLE);
		holder.price3.setVisibility(View.INVISIBLE);
		holder.price0.setSelected(false);
		holder.price1.setSelected(false);
		holder.price2.setSelected(false);
		holder.price3.setSelected(false);
		for (int i = 0; i < stampShop.size(); i++) {
			HashMap<Integer, Integer> chooseData = activity.getChooseData();
			StampShopBean shopBean = stampShop.get(i);
			LinearLayout view = null;
			switch (i) {
			case 0:
				view = holder.price0;
				break;
			case 1:
				view = holder.price1;
				break;
			case 2:
				view = holder.price2;
				break;
			case 3:
				view = holder.price3;
				break;
			default:
				break;
			}
			view.setSelected(false);
			if (chooseData.containsKey(position)) {
				Integer integer = chooseData.get(position);
				if (integer == i) {
					view.setSelected(true);
				} else {
					view.setSelected(false);
				}
			}
			view.setVisibility(View.VISIBLE);
			for (int p = 0; p < view.getChildCount(); p++) {
				if (p == 0) {
					TextView saleCount = (TextView) view.getChildAt(p);
					saleCount.setText(shopBean.getStampNum() + "张");
				} else {
					TextView salePrice = (TextView) view.getChildAt(p);
					salePrice.setText("单价￥" + shopBean.getSimplePrice());
				}

			}
			final int j = i;
			view.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					HashMap<Integer, Integer> chooseData = activity.getChooseData();
					if (chooseData.containsKey(position)) {// 是否已经选过
						Integer integer = chooseData.get(position);
						if (integer == j) {// 选过在点击取消
							chooseData.remove(position);
							// view.setSelected(false);
						} else {
							chooseData.put(position, j);
						}
					} else {
						chooseData.put(position, j);
					}
					activity.setChooseData(chooseData);
					activity.setChooseDataChange();// 跟新购物车
					notifyDataSetChanged();// 跟新item
				}
			});
		}
		return convertView;
	}

	private class ViewHolder {
		private ImageView stampImg;// 邮票
		private TextView stampName; // 邮票名称
		private TextView desc; // 邮票描述
		private TextView deadline;// 有效期至
		private TextView category;// 系列
		private LinearLayout price1;
		private LinearLayout price2;
		private LinearLayout price3;
		private LinearLayout price0;
	}

}
